package leetcode.array;

/**
 * @author 陈正龙
 * @date 2022/7/22 11:01
 * 59. 螺旋矩阵 II
 */
public class _59 {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int current = 2;
        arr[0][0] = 1;
        int x = 0;
        int y = 0;
        while (current <= n * n) {
            // 右
            while (x < n - 1 && arr[y][x + 1] == 0) {
                arr[y][x + 1] = current;
                current++;
                x++;
            }

            // 下
            while (y < n - 1 && arr[y + 1][x] == 0) {
                arr[y + 1][x] = current;
                current++;
                y++;
            }
            // 左
            while (x > 0 && arr[y][x - 1] == 0) {
                arr[y][x - 1] = current;
                current++;
                x--;
            }
            // 上
            while (y > 0 && arr[y - 1][x] == 0) {
                arr[y - 1][x] = current;
                current++;
                y--;
            }
        }
        return arr;
    }

}
